package com.androidexplosion.takeitback.gui;

import java.sql.Date;
import java.sql.Time;

import com.androidexplosion.takeitback.R;
import com.androidexplosion.takeitback.provider.RemindDBAdapter;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class AddRemind extends Activity {

	static final int PICK_DATE_DIALOG = 2;
	static final int PICK_CONTACT_DIALOG = 1;

	private int year;
	private int month;
	private int day;
	RemindDBAdapter dbAdapter;

	EditText txtObject;
	TextView txtDate;
	TimePicker tpOra;
	EditText txtContact;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.addremind);

		txtObject = (EditText) findViewById(R.id.txtObject);
		txtDate = (TextView) findViewById(R.id.txtDate);
		tpOra = (TimePicker) findViewById(R.id.tpOra);
		txtContact = (EditText) findViewById(R.id.txtContact);
		
		Date date = new Date(System.currentTimeMillis());
		
		year = date.getYear() + 1900;
		month = date.getMonth();
		day = date.getDay();
		
		updateDate();

		dbAdapter = new RemindDBAdapter(this);
		
		dbAdapter.open();
	}

	@Override
	public void onActivityResult(int reqCode, int resCode, Intent data) {
		super.onActivityResult(reqCode, resCode, data);

		switch (reqCode) {
		case (PICK_CONTACT_DIALOG): {
			if (resCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = managedQuery(contactData, null, null, null, null);
				c.moveToFirst();
				// int id = c.getInt(c.getColumnIndexOrThrow(People._ID));
				String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
				txtContact.setText(name);
			}
			break;
		}
		}
	}

	public void btnPickContact_onClick(View _view) {
		Intent intent = new Intent(Intent.ACTION_PICK,
				Contacts.People.CONTENT_URI);
		startActivityForResult(intent, PICK_CONTACT_DIALOG);
	}

	public void btnSave_onClick(View _view) {
		try
		{
		
		
		String object = txtObject.getText().toString();
		Date date = new Date(year, month, day);
		Time time = new Time(tpOra.getCurrentHour(), tpOra.getCurrentMinute(),
				0);
		String person = txtContact.getText().toString();

		dbAdapter.addRemind(object, person, date.getTime(), time.getTime());

		finish();
		}
		catch(Exception ex)
		{
			Log.e("Fuck", ex.getMessage() + "/n" + ex.getStackTrace());
		}
	}

	public void btnPickDate_onClick(View _view) {
		showDialog(PICK_DATE_DIALOG);
	}

	private void updateDate() {
		txtDate.setText(new StringBuilder()
				// Month is 0 based so add 1
				.append(day).append("-").append(month + 1).append("-")
				.append(year).append(" "));
	}

	// the callback received when the user "sets" the date in the dialog
	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker _view, int _year, int _monthOfYear,
				int _dayOfMonth) {
			year = _year;
			month = _monthOfYear;
			day = _dayOfMonth;
			updateDate();
		}
	};
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case PICK_DATE_DIALOG:
	        return new DatePickerDialog(this,
	                    mDateSetListener,
	                    year, month, day);
	    }
	    return null;
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();

		// Close the database
		dbAdapter.close();
	}

	// @Override
	// protected void onPause() {
	// super.onPause();
	//		
	// }
	//
	// @Override
	// protected void onResume() {
	// super.onPause();
	//		
	// }

}
